package com.axdoc.wallet.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.axdoc.wallet.dao.IDoctorWalletDao;
import com.axdoc.wallet.entity.DoctorBalanceSubWallet;
import com.axdoc.wallet.entity.DoctorBalanceSubWalletRecords;
import com.axdoc.wallet.entity.DoctorWallet;

@Component("doctorWalletDao")
public class DoctorWalletDaoImpl implements IDoctorWalletDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean addDoctorWallet(DoctorWallet doctorWallet) {
		boolean result = false;
		/* 插入实现 */
		int i = jdbcTemplate.update("insert into doctor_wallet(wallet_no,docid,wallet_type,p_no) values(?,?,?,?)",
				new Object[] { doctorWallet.getWalletNo(), doctorWallet.getDocid(), doctorWallet.getWalletType(),
						doctorWallet.getpNo() });
		if (i > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean addDoctorBalanceSubWallet(DoctorBalanceSubWallet doctorBalanceSubWallet) {
		boolean result = false;
		/* 插入实现 */
		int i = jdbcTemplate.update(
				"insert into doctor_balance_sub_wallet(wallet_no,m_wallet_no,pay_password,is_allowed_pay,is_allowed_recharge,is_allowed_withdrawal,state,balance,withdrawal_balance_can,withdrawal_balance_wait,docid,forzen_balance) values(?,?,?,?,?,?,?,?,?,?,?,?)",
				new Object[] { doctorBalanceSubWallet.getWalletNo(), doctorBalanceSubWallet.getmWalletNo(),
						doctorBalanceSubWallet.getPayPassword(), doctorBalanceSubWallet.getIsAllowedPay(),
						doctorBalanceSubWallet.getIsAllowedRecharge(), doctorBalanceSubWallet.getIsAllowedWithdrawal(),
						doctorBalanceSubWallet.getState(), doctorBalanceSubWallet.getBalance(),
						doctorBalanceSubWallet.getWithdrawalBalanceCan(),
						doctorBalanceSubWallet.getWithdrawalBalanceWait(),doctorBalanceSubWallet.getDocid(),doctorBalanceSubWallet.getForzenBalance() });
		if (i > 0) {
			result = true;
		}
		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DoctorWallet queryDoctorWalletByDocid(String docid) {
		List<DoctorWallet> list = new ArrayList<DoctorWallet>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from doctor_wallet where docid = ? ");
		list = (List<DoctorWallet>) jdbcTemplate.query(sql.toString(), new Object[] { docid }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				DoctorWallet dw = new DoctorWallet();
				dw.setId(rs.getInt("id"));
				dw.setWalletNo(rs.getString("wallet_no"));
				dw.setDocid(rs.getString("docid"));
				dw.setWalletType(rs.getInt("wallet_type"));
				dw.setpNo(rs.getString("p_no"));
				dw.setCreateTime(rs.getTimestamp("create_time"));
				dw.setUpdateTime(rs.getTimestamp("update_time"));
				return dw;
			}
		});
		if(null != list && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DoctorBalanceSubWallet queryDoctorBalanceSubWalletByDocid(String docid) {
		List<DoctorBalanceSubWallet> list = new ArrayList<DoctorBalanceSubWallet>();
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select * from doctor_balance_sub_wallet where docid = ? ");
		list = (List<DoctorBalanceSubWallet>) jdbcTemplate.query(sql.toString(), new Object[] { docid }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				DoctorBalanceSubWallet subWallet = new DoctorBalanceSubWallet();
				subWallet.setId(rs.getInt("id"));
				subWallet.setWalletNo(rs.getString("wallet_no"));
				subWallet.setmWalletNo(rs.getString("m_wallet_no"));
				subWallet.setPayPassword(rs.getString("pay_password"));
				subWallet.setIsAllowedPay(rs.getInt("is_allowed_pay"));
				subWallet.setIsAllowedRecharge(rs.getInt("is_allowed_recharge"));
				subWallet.setIsAllowedWithdrawal(rs.getInt("is_allowed_withdrawal"));
				subWallet.setState(rs.getInt("state"));
				subWallet.setBalance(rs.getInt("balance"));
				subWallet.setWithdrawalBalanceCan(rs.getInt("withdrawal_balance_can"));
				subWallet.setWithdrawalBalanceWait(rs.getInt("withdrawal_balance_wait"));
				subWallet.setCreateTime(rs.getTimestamp("create_time"));
				subWallet.setUpdateTime(rs.getTimestamp("update_time"));
				subWallet.setDocid(rs.getString("docid"));
				subWallet.setForzenBalance(rs.getInt("forzen_balance"));
				return subWallet;
			}
		});
		if(null != list && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean updateDoctorBalanceSubWallet(DoctorBalanceSubWallet doctorBalanceSubWallet) {
		boolean result = false;
		/* 更新实现 */
		int i = jdbcTemplate.update(
				"update doctor_balance_sub_wallet set wallet_no=?,m_wallet_no=?,pay_password=?,is_allowed_pay=?,is_allowed_recharge=?,is_allowed_withdrawal=?,state=?,balance=?,withdrawal_balance_can=?,withdrawal_balance_wait=?,forzen_balance=? where id=?",
				new Object[] { doctorBalanceSubWallet.getWalletNo(), doctorBalanceSubWallet.getmWalletNo(),
						doctorBalanceSubWallet.getPayPassword(), doctorBalanceSubWallet.getIsAllowedPay(),
						doctorBalanceSubWallet.getIsAllowedRecharge(), doctorBalanceSubWallet.getIsAllowedWithdrawal(),
						doctorBalanceSubWallet.getState(), doctorBalanceSubWallet.getBalance(),
						doctorBalanceSubWallet.getWithdrawalBalanceCan(),
						doctorBalanceSubWallet.getWithdrawalBalanceWait(),doctorBalanceSubWallet.getForzenBalance(),doctorBalanceSubWallet.getId() });
		if (i > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean addBalanceRecords(DoctorBalanceSubWalletRecords balance) {
		boolean result = false;
		/* 插入实现 */
		int i = jdbcTemplate.update(
				"insert into doctor_balance_sub_wallet_records(wallet_no,trade_no,trx_no,biz_no,change_amount,change_direction,trx_code,trx_desc,biz_code,biz_desc,docid,recorded_time,biz_type,income_id,detail_id,detail_type) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
				new Object[] { balance.getWalletNo(), balance.getTradeNo(), balance.getTrxNo(), balance.getBizNo(),
						balance.getChangeAmount(), balance.getChangeDirection(), balance.getTrxCode(),
						balance.getTrxDesc(), balance.getBizCode(), balance.getBizDesc(),balance.getDocid(),balance.getRecordedTime(),balance.getBizType(),balance.getIncomeId(),balance.getDetailId(),balance.getDetailType()});
		if (i > 0) {
			result = true;
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<DoctorBalanceSubWalletRecords> queryBalanceRecords(String bizNo, Timestamp startTime,
			Timestamp endTime) {
		List<DoctorBalanceSubWalletRecords> list = new ArrayList<DoctorBalanceSubWalletRecords>();
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select * from doctor_balance_sub_wallet_records where biz_no = ? ");
		list = (List<DoctorBalanceSubWalletRecords>) jdbcTemplate.query(sql.toString(), new Object[] { bizNo }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				DoctorBalanceSubWalletRecords record = new DoctorBalanceSubWalletRecords();
				record.setId(rs.getInt("id"));
				record.setWalletNo(rs.getString("wallet_no"));
				record.setTradeNo(rs.getString("trade_no"));
				record.setTrxNo(rs.getString("trx_no"));
				record.setBizNo(rs.getString("biz_no"));
				record.setChangeAmount(rs.getInt("change_amount"));
				record.setChangeDirection(rs.getInt("change_direction"));
				record.setTrxCode(rs.getString("trx_code"));
				record.setTrxDesc(rs.getString("trx_desc"));
				record.setBizCode(rs.getString("biz_code"));
				record.setBizDesc(rs.getString("biz_desc"));
				record.setCreateTime(rs.getTimestamp("create_time"));
				record.setUpdateTime(rs.getTimestamp("update_time"));
				record.setDocid(rs.getString("docid"));
				record.setRecordedTime(rs.getTimestamp("recorded_time"));
				return record;
			}
		});
		return list;
	}
	
	@Override
	public boolean updateBalanceById(int id, int balance) {
		boolean result = false;
		/* 更新实现 */
		int i = jdbcTemplate.update("update doctor_balance_sub_wallet set balance=balance+? where id=?",
				new Object[] { balance, id });
		if (i > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean updateFrozenBalanceById(int id, int frozenBalance) {
		boolean result = false;
		/* 更新实现 */
		int i = jdbcTemplate.update("update doctor_balance_sub_wallet set forzen_balance=forzen_balance+? where id=?",
				new Object[] { frozenBalance, id });
		if (i > 0) {
			result = true;
		}
		return result;
	}
	
}
